//
//  AlarmView.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/17.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "AlarmView.h"
#import "YSCommon.h"
#import "DeviceControlController.h"

@interface AlarmView ()
@property (nullable, nonatomic, strong) UIImageView* mImgIcon;
@property (nullable, nonatomic, strong) UILabel* mLblContent;
@property (nullable, nonatomic, strong) UIButton* mBtnWatch;
@property (nullable, nonatomic, strong) UIButton* mBtnSure;
@property (nullable, nonatomic, strong) UIView* mViewSplitH;
@property (nullable, nonatomic, strong) UIView* mViewSplitV;

@end
@implementation AlarmView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark - event response
- (void)didTappedButton:(UIButton*)button
{
    if (button == self.mBtnWatch)
    {
        UIViewController* vc =  [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
        DeviceControlController* controlVc = [[DeviceControlController alloc]init];
        controlVc.device = self.device;
        [vc.navigationController pushViewController:controlVc animated:YES];
    }
    else if (button == self.mBtnSure)
    {
         [self.device setCircuitStatus:0 which:1];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(alarmViewDidTappedHandle:)]) {
        [self.delegate alarmViewDidTappedHandle:self];
    }
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]])
    {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    }
    else if ([rootViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    }
    else if (rootViewController.presentedViewController)
    {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    }
    else
    {
        return rootViewController;
    }
}
#pragma mark - private method
- (void)initSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.mImgIcon];
    [self addSubview:self.mLblContent];
    [self addSubview:self.mBtnWatch];
    [self addSubview:self.mBtnSure];
    [self addSubview:self.mViewSplitH];
    [self addSubview:self.mViewSplitV];
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.mImgIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self).offset(UIEdgeInsetsMake(10, 10, 0, 0));
        make.width.equalTo(kIphoneSize_Widith(22));
        make.height.equalTo(kIphoneSize_Height(22));
    }];
    [self.mLblContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mImgIcon.trailing).offset(5);
        make.top.equalTo(self.mImgIcon.top);
        make.trailing.equalTo(self.trailing).offset(-5);
    }];
    [self.mViewSplitH mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mLblContent.bottom).offset(10);
        make.leading.trailing.equalTo(self);
        make.height.equalTo(1);
    }];
    [self.mBtnWatch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mViewSplitH.bottom);
        make.leading.equalTo(self.leading);
        make.width.equalTo(self).multipliedBy(0.5);
        make.height.equalTo(50);
    }];
    [self.mViewSplitV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mBtnWatch);
        make.leading.equalTo(self.mBtnWatch.trailing);
        make.width.equalTo(1);
    }];
    [self.mBtnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mBtnWatch);
        make.leading.equalTo(self.mViewSplitV.trailing);
        make.trailing.equalTo(self);
        make.height.equalTo(self.mBtnWatch);
    }];
    [self.mBtnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
}

#pragma mark - getters and setters

- (void)setDevice:(ZTOneControlDevice *)device
{
    _device = device;
    NSString* content = [NSString stringWithFormat:@"%@触发",[device getDeviceName]];
    self.mLblContent.text = content;
}
-(UIImageView *)mImgIcon
{
    if (!_mImgIcon) {
        _mImgIcon = [[UIImageView alloc]init];
        _mImgIcon.image = [UIImage imageNamed:@"newdevice_icon_find"];
    }
    return _mImgIcon;
}
- (UILabel *)mLblContent
{
    if (!_mLblContent) {
        _mLblContent = [[UILabel alloc]init];
        _mLblContent.numberOfLines = 0;
    }
    return _mLblContent;
}
-(UIButton *)mBtnWatch
{
    if (!_mBtnWatch)
    {
        _mBtnWatch = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mBtnWatch setTitle:@"查看" forState:UIControlStateNormal];
        [_mBtnWatch setTitleColor:RGBColor(239, 75, 58) forState:UIControlStateNormal];
        [_mBtnWatch addTarget:self action:@selector(didTappedButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mBtnWatch;
}
-(UIButton *)mBtnSure
{
    if (!_mBtnSure)
    {
        _mBtnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mBtnSure setTitle:@"确定" forState:UIControlStateNormal];
        [_mBtnSure setTitleColor:RGBColor(239, 75, 58) forState:UIControlStateNormal];
        [_mBtnSure addTarget:self action:@selector(didTappedButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mBtnSure;
}
- (UIView *)mViewSplitH
{
    if (!_mViewSplitH) {
        _mViewSplitH = [[UIView alloc]init];
        _mViewSplitH.backgroundColor = RGBColor(225, 225, 225);
    }
    return _mViewSplitH;
}
- (UIView *)mViewSplitV
{
    if (!_mViewSplitV) {
        _mViewSplitV = [[UIView alloc]init];
        _mViewSplitV.backgroundColor = RGBColor(225, 225, 225);
    }
    return _mViewSplitV;
}
@end
